package com.kingdee.eas.custom.aoyuan.aytime.util;

import java.util.Calendar;
import java.util.Date;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.ui.face.ItemAction;
import com.kingdee.eas.base.permission.PermissionFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.CtrlUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitFactory;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgUnitRelationCollection;
import com.kingdee.eas.basedata.org.OrgUnitRelationInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.framework.client.CoreUI;
import com.kingdee.eas.util.SysUtil;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.jdbc.rowset.IRowSet;

public class ManagerUtil {
   public static boolean getManagerPermission(String premItemName)
   {   
	   boolean hasPermission=false;
	   UserInfo userInfo =SysContext.getSysContext().getCurrentUserInfo();
	   CtrlUnitInfo cuOrg= userInfo.getCU();
	   //premItemName="manager001";

	   try {
		   hasPermission=PermissionFactory.getRemoteInstance().hasFunctionPermission(new ObjectUuidPK(userInfo.getId()), new ObjectUuidPK(cuOrg.getId()), premItemName);
	} catch (EASBizException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (BOSException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
	   return hasPermission;
   }
   
   public static FilterInfo getInfoUser(Date startDate,Date endDate)
   {
	   Calendar c = Calendar.getInstance();
	   c.setTime(startDate);
	   c.set(Calendar.HOUR, 0);
	   c.set(Calendar.MINUTE, 0);
	   c.set(Calendar.SECOND, 0);
	   startDate = c.getTime();
	   c.setTime(endDate);
	   c.set(Calendar.HOUR, 23);
	   c.set(Calendar.MINUTE, 59);
	   c.set(Calendar.SECOND, 59);
	   endDate = c.getTime();
 	  UserInfo userInfo =SysContext.getSysContext().getCurrentUserInfo();
	  FilterInfo filterInfo=new FilterInfo();
	  filterInfo.getFilterItems().add(new FilterItemInfo("Emp.id",userInfo.getId().toString(),CompareType.EQUALS));
      filterInfo.getFilterItems().add(new FilterItemInfo("bizDate",startDate,CompareType.GREATER_EQUALS));
      
      filterInfo.getFilterItems().add(new FilterItemInfo("bizDate",endDate,CompareType.LESS_EQUALS));	   
      return filterInfo;
   }
  public static FilterInfo getInfoManager(Date startDate,Date endDate)
   {
 	  UserInfo userInfo =SysContext.getSysContext().getCurrentUserInfo();
 	 Calendar c = Calendar.getInstance();
	   c.setTime(startDate);
	   c.set(Calendar.HOUR, 0);
	   c.set(Calendar.MINUTE, 0);
	   c.set(Calendar.SECOND, 0);
	   startDate = c.getTime();
	   c.setTime(endDate);
	   c.set(Calendar.HOUR, 23);
	   c.set(Calendar.MINUTE, 59);
	   c.set(Calendar.SECOND, 59);
	   endDate = c.getTime();
	  FilterInfo filterInfo=new FilterInfo();
	  filterInfo.getFilterItems().add(new FilterItemInfo("CU.id",userInfo.getCU().getId().toString(),CompareType.EQUALS));
      filterInfo.getFilterItems().add(new FilterItemInfo("bizDate",startDate,CompareType.GREATER_EQUALS));
      filterInfo.getFilterItems().add(new FilterItemInfo("bizDate",endDate,CompareType.LESS_EQUALS));	   
      return filterInfo;
   }  
  public static FilterInfo getInfoManager()
  {
	  UserInfo userInfo =SysContext.getSysContext().getCurrentUserInfo();
	  FilterInfo filterInfo=new FilterInfo();
	  filterInfo.getFilterItems().add(new FilterItemInfo("CU.id",userInfo.getCU().getId().toString(),CompareType.EQUALS));   
     return filterInfo;
  }  
  public static HROrgUnitInfo getCurrentHROrg()
  {
	  try
	  {
		  HROrgUnitInfo hrOrg = SysContext.getSysContext().getCurrentHRUnit();
		  if (hrOrg != null && hrOrg.isIsBizUnit() )
		  {
			  return hrOrg;
		  }
		  else
		  {
			  AdminOrgUnitInfo adminOrg = SysContext.getSysContext().getCurrentAdminUnit();
			  if (adminOrg == null )return null;
			  String strSQL = "select a.FToUnitID from T_ORG_UnitRelation  a\r\n " + 
			  					"	 inner join  T_ORG_TypeRelation b on a.FTypeRelationID = b.FID \r\n " + 
			  					"  where b.FFromType ='0' and b.FToType = '16' \r\n " + 
			  					"  And a.FFromUnitID='" + adminOrg.getId().toString() + "'";
			  IRowSet rs =SQLExecutorFactory.getRemoteInstance(strSQL).executeSQL() ;
			  if (rs.next())
			  {
				  String hrid = rs.getString("FToUnitID");
				  hrOrg = HROrgUnitFactory.getRemoteInstance().getHROrgUnitInfo(new ObjectUuidPK(hrid));
				  return hrOrg;
			  }
//			  rs.close();
		  }
	  }
	  catch(Exception ex)
	  {
		  ex.printStackTrace();
	  }
	  return null;
  }
}
